package com.project.erp.common.utils.encode;

import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：MD5消息摘要算法工具类

 *  功能描述：

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2012-10-19 上午21:53
 */
public class MD5Util {
	
	private static Logger LOGGER  = LoggerFactory.getLogger(MD5Util.class);
	
	public static String getMD5String(String str){
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = str.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char strChar[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];
				strChar[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(strChar);
		} catch (Exception e) {
			LOGGER.error("MD5加密出错！");
			return null;
		}

	}
}
